﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.DAL.DocumentCenter;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Linq;

namespace MS.InformationManagement.BLL.GestionPerfiles
{
    public class GestionPerfiles
    {
        public static IEnumerable<VincularPerfiles> GetPerfiles()
        {
            DCDocumentCenterDataContext dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url);
            
            IEnumerable<VincularPerfiles> EntidadesPerfiles = (from item in dc.Perfiles
                         orderby item.Perfil
                         group item by item.Perfil
                             into g
                             select g.First());

            return EntidadesPerfiles;
        }

        //public static List<VincularPerfiles> GetRolesByPerfil(string Perfil)
        //{

        //    SPList ListPerfilRoles = SPContext.Current.Web.Lists.TryGetList(GlobalConstants.LIST_PERFILES_ROL);
        //    List<VincularPerfiles> entidades = null;
        //    if (ListPerfilRoles != null)
        //    {
        //        var result = (from SPListItem item in ListPerfilRoles.GetItems()
        //                     where item[CustomSiteColumns.FIELD_INTERNAL_PERFIL].ToString() == Perfil
        //                     orderby item[CustomSiteColumns.FIELD_INTERNAL_PERFIL]
        //                     select new VincularPerfiles
        //                     {
        //                         Perfil = item[CustomSiteColumns.FIELD_INTERNAL_PERFIL].ToString(),
        //                         TipoRole = getRol(item),
        //                         Id = item.ID
        //                     });

        //        if (result.Count() > 0)
        //        {
        //            entidades = result.ToList();
        //        }
        //    }
           

        //    return entidades;
        //}

        public static List<ListItem> getAllRoles()
        {
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                return (from rol in dc.Roles
                        select new ListItem { Value = rol.Id.ToString(), Text = rol.Role }).ToList();
            }
        }

        private static Roles getRol(SPListItem item)
        {
            
            SPFieldLookupValue rol = new SPFieldLookupValue(item[CustomSiteColumns.FIELD_DISPLAY_ROLE_LOOKUP] +"");
            if (rol != null)
            {
                return new Roles() { Id = rol.LookupId, Role = rol.LookupValue };
            }
            else
            {
                return new Roles() { Id = 0, Role = string.Empty };
            }
            
        }

        private static Roles getRolLookupValue(ListItem item)
        {
            return new Roles() { Id = int.Parse(item.Value), Role = item.Text };
        }

        public static bool DeletePerfil(string Perfil)
        {
            bool result = false;
            DCDocumentCenterDataContext dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url);
            EntityList<VincularPerfiles> listPerfiles = dc.GetList<VincularPerfiles>(GlobalConstants.LIST_PERFILES_ROL);
            var itemsToDelete = (from r in dc.Perfiles where r.Perfil == Perfil select r);
            if (itemsToDelete != null)
            {
                foreach (VincularPerfiles PerfilToDelete in itemsToDelete)
                {
                    listPerfiles.DeleteOnSubmit(PerfilToDelete);
                    
                }
            }
            dc.SubmitChanges();
            result = true;
            return result;
        }

        public static bool UpdatePerfil(string Perfil, List<ListItem> newRolList)
        {
            bool result = false;
            DeletePerfil(Perfil);

            SPList ListPerfilRoles = SPContext.Current.Web.Lists.TryGetList(GlobalConstants.LIST_PERFILES_ROL);

            foreach (ListItem itemRole in newRolList)
            {
                SPListItem item = ListPerfilRoles.Items.Add();
                SPFieldLookupValueCollection fieldValues = new SPFieldLookupValueCollection();
                fieldValues.Add(new SPFieldLookupValue(int.Parse(itemRole.Value), itemRole.Text));  // add other field values or in loop
                item[CustomSiteColumns.FIELD_DISPLAY_ROLE_LOOKUP] = fieldValues;
                item[CustomSiteColumns.FIELD_INTERNAL_PERFIL] = Perfil;
                item.Update(); 
            }
            result = true;
            return result;
        }

        public static bool CreatePerfil(string Perfil)
        {
            bool result = false;
            SPList ListPerfilRoles = SPContext.Current.Web.Lists.TryGetList(GlobalConstants.LIST_PERFILES_ROL);

            SPListItem item = ListPerfilRoles.Items.Add();
            item[CustomSiteColumns.FIELD_INTERNAL_PERFIL] = Perfil;
            item.Update();

            result = true;
            return result;
        }

    }
}
